package pers.wensheng.patterns.creator_pattern.singleton_pattern.destroy_and_solve.reflex_mode;

public class Singleton {

    //防止破环单例模式
    private static boolean flag = false;

    /**
     *  解决
     *  防止破环单例模式
     */
    private Singleton(){
        synchronized (Singleton.class){
            if (flag) {
                throw  new RuntimeException("不能创建多个对象");
            }
            flag = true;
        }
    }

    public static Singleton getInstance(){
        return SingletonHolder.INSTANCE;
    }

    //外界不能访问
    private static class SingletonHolder {
        //在内部类中声名并初始化外部类的对象
        public static final Singleton INSTANCE = new Singleton();
    }
}
